package com.example.user.autocompletedemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class AutoCompleteTextViewActivity extends AppCompatActivity {
    private static final String[] autoStr = new String[]{"abc","abcd","abcde"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //载入main.xml Layout
        setContentView(R.layout.activity_autocompletetextview);
        //new ArrayAdapter对象并将autoStr字符串数组传入
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,autoStr);
        //以findViewById取得AutoCompleteTextView对象
        AutoCompleteTextView myAutoCompleteTextView =(AutoCompleteTextView)findViewById(R.id.myAutoCompleteTextView);
        //将ArrayAdapter假如AutoCompleteTextView对象中
        myAutoCompleteTextView.setAdapter(adapter);

        /*
        * 用MultiAutoCompleteTextView实现
        * MultiAutoCompleteTextView与AutoCompleteTextView区别：可以在输入框中一直增加新的选项值
        * 进行setAdapter之后需要调用setTokenizer(),否则会出错
        * */
        MultiAutoCompleteTextView myMultiAutoCompleteTextView = (MultiAutoCompleteTextView)
                findViewById(R.id.myMultiAutoCompleteTextView);
        myMultiAutoCompleteTextView.setAdapter(adapter);
        myMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}
